package de.slothsoft.wasbinich.android;

import java.util.Locale;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import de.slothsoft.android.util.WebUtil;
import de.slothsoft.wasbinich.android.database.webservice.WebserviceDatabase;
import de.slothsoft.wasbinich.android.preference.PreferenceConstants;
import de.slothsoft.wasbinich.android.preference.PreferenceUtil;
import de.slothsoft.wasbinich.android.util.ActivityUtil;
import de.slothsoft.wasbinich.android.util.MediaPlayerUtil;
import de.slothsoft.wasbinich.android.util.StyleUtil;
import de.slothsoft.wasbinich.android.util.SvgUtil;
import de.slothsoft.wasbinich.core.WasBinIch;
import de.slothsoft.wasbinich.core.WasBinIch.Version;
import de.slothsoft.wasbinich.core.database.Database;

public abstract class AbstractMainActivity extends AbstractActivity implements OnClickListener {

    protected static final String URL_FORUM = "http://wasbinich.slothsoft.de/forum/wasbinich"; //$NON-NLS-1$

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	initApplication();
	initLocale();
	initMusic();
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	hookButtonListener();

	ImageView mascotView = (ImageView) findViewById(R.id.main_mascot);
	SvgUtil.setSvgToImageView(this, mascotView, getMascotImage());
    }

    private void initApplication() {
	if (!WasBinIch.isInitialized()) {
	    WasBinIch.initVersion(getVersion());
	    Database.initInstance(new WebserviceDatabase());
	}
    }

    protected abstract Version getVersion();

    private void initLocale() {
	String localeString = PreferenceUtil.getCurrentValue(this, PreferenceConstants.PREF_LANGUAGE, ""); //$NON-NLS-1$
	if (localeString != null && !localeString.equals("")) { //$NON-NLS-1$
	    Locale locale = new Locale(localeString);
	    Locale.setDefault(locale);
	    ActivityUtil.reloadLocale(this, locale);
	}
    }

    private void initMusic() {
	MediaPlayerUtil.userWantsMusic = PreferenceUtil.getCurrentBoolean(this, PreferenceConstants.PREF_MUSIC, true);
    }

    protected abstract int getMascotImage();

    private void hookButtonListener() {
	// edit onClick(...) next!
	hookButtonListener(R.id.main_start);
	hookButtonListener(R.id.main_settings);
	hookButtonListener(R.id.main_info);
	hookButtonListener(R.id.main_pictures);
	// hookButtonListener(R.id.main_highscore);
	removeLiteButtons();
    }

    private void hookButtonListener(int id) {
	Button button = (Button) findViewById(id);
	button.setOnClickListener(this);
	StyleUtil.styleButton(this, button);
    }

    private void removeLiteButtons() {
	if (WasBinIch.getVersion() == Version.LITE) {
	    Button button = (Button) findViewById(R.id.main_pictures);
	    ViewGroup layout = (ViewGroup) button.getParent();
	    if (null != layout) {
		layout.removeView(button);
	    }
	}
    }

    @Override
    public void onClick(View view) {
	int id = view.getId();

	if (id == R.id.main_settings) {
	    startActivity(new Intent(this, SettingsActivity.class));
	} else if (id == R.id.main_info) {
	    startActivity(new Intent(this, InfoActivity.class));
	} else {
	    if (!WebUtil.isOnline(this)) {
		showOfflineErrorMessage();
		return;
	    }

	    if (id == R.id.main_start) {
		startActivity(new Intent(this, QuestionActivity.class));
	    } else if (id == R.id.main_settings) {
		startActivity(new Intent(this, SettingsActivity.class));
	    } else if (id == R.id.main_pictures) {
		startActivity(new Intent(this, PictureListActivity.class));
		// } else if (id == R.id.main_highscore) {
		// startActivity(new Intent(this, HighscoreActivity.class));
	    }
	}
    }

    private void showOfflineErrorMessage() {
	AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(R.string.main_error_offline_title)
		.setMessage(R.string.main_error_offline)
		.setPositiveButton(R.string.main_error_forum, new Dialog.OnClickListener() {

		    @Override
		    public void onClick(DialogInterface dialogInterface, int i) {
			WebUtil.openUrl(AbstractMainActivity.this, URL_FORUM);
		    }
		}).setNegativeButton(R.string.facebook_ok, new Dialog.OnClickListener() {

		    @Override
		    public void onClick(DialogInterface dialogInterface, int i) {
			dialogInterface.dismiss();
		    }
		});
	builder.create().show();
    }
}
